<?php

namespace app\money\controller;

use app\admin\common\Base;
use think\Db;
// 其他收支明细表
class Otherincomedetail extends Base
{
    public function index()
    {
        $data = $this->request->param();

        if (isset($data['ajax']) && $data['ajax'] == 100) {
            $map = '';
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);

            // if ($map == '') {
            //     $list = Db::name('income')->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->order('id desc')->limit($start, $limit)->select();
            //     $count = Db::name('income')->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
            // } else {
            //     $list = Db::name('income')->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->order('id desc')->limit($start, $limit)->select();
            //     $count = Db::name('income')->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
            // }
            $sql = search('income', $data);
            $list = $sql->limit($start, $limit)->select();
            $count = $sql->count();

            foreach ($list as $key => $value) {
                $list[$key]['dj_time'] = date('Y-m-d', $value['dj_time']);
                $income_type_id = Db::name('income_good')->where('pid', $value['id'])->value('income_type_id');
                switch ($value['type']) {
                    case 0:
                        $list[$key]['type_name'] = '其他收入';
                        $list[$key]['income_type_name'] = Db::name('income_type')->where('id', $income_type_id)->value('name');
                        $list[$key]['shouru'] = $value['now_jine'];
                        break;
                    case 1:
                        $list[$key]['type_name'] = '其他支出';
                        $list[$key]['income_type_name'] = Db::name('money_type')->where('id', $income_type_id)->value('name');
                        $list[$key]['zhichu'] = $value['now_jine'];
                        break;
                    default:
                        # code...
                        break;
                }
                $list[$key]['user_name'] = Db::name('customer')->where('id', $value['user_id'])->value('name');
            }
            // halt($list);
            return $this->grid_data($list, $count, $page, $limit);
        }
        $income_type = Db::name('income_type')->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->select();
        $money_type = Db::name('money_type')->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->select();
        $month = $this->get_month_start_to_end();
        $this->assign('income_type', $income_type);
        $this->assign('money_type', $money_type);
        $this->assign('month', $month);

        return $this->fetch();
    }
}
